﻿myApp.factory('executeCommand', ['$http', function ($http) {
    return function (key, param, callback) {
        $http({
            url: '/Interface/ajax/CommandService.aspx?key=' + key,
            method: 'POST',
            params: { param: param }
        }).then(function successCallback(response) {
        	console.log(response.data)
            if (response.data.ErrorCode == '0000') {
                callback(response.data);
            }
        }, function errorCallback(response) {
        })
    }
}
])
myApp.factory('firstFilePath', function () {
    return function (json) {
        try {
            var fileArr = $.parseJSON(json);
            return ($.cookie('imagesvr') ? $.cookie('imagesvr') : '') + fileArr[0].FilePath;
        } catch (e) {
//          console.log(json);
            return '';
        }
    }
})
myApp.factory('firstThumbnail', function () {
    return function (json) {
        try {
            var fileArr = $.parseJSON(json);
            return ($.cookie('imagesvr') ? $.cookie('imagesvr') : '') + fileArr[0].FilePath + '.thumb.' + fileArr[0].FileExt;
        } catch (e) {
//          console.log(json);
            return '';
        }
    }
})
myApp.factory('stateService', function () {

    var _states = {};

    var _save = function (name, scope, fields) {
        //        if (!_states[name])
        //            _states[name] = {};
        //        for (var i = 0; i < fields.length; i++) {
        //            _states[name][fields[i]] = scope[fields[i]];
        //        }
        sessionStorage.setItem(name, '1');
        for (var i = 0; i < fields.length; i++) {
            sessionStorage.setItem(name + '_' + fields[i], JSON.stringify(scope[fields[i]]));
        }
    }
    var _load = function (name, scope, fields) {
        //        if (!_states[name])
        //            return scope;
        //        for (var i = 0; i < fields.length; i++) {
        //            if (typeof _states[name][fields[i]] !== 'undefined')
        //                scope[fields[i]] = _states[name][fields[i]];
        //        }
        if (sessionStorage.getItem(name) != '1')
            return scope;
        for (var i = 0; i < fields.length; i++) {
            var json = sessionStorage.getItem(name + '_' + fields[i]);
            if (json)
                scope[fields[i]] = JSON.parse(json);
        }
        return scope;
    }

    // ===== Return exposed functions ===== //
    return ({
        save: _save,
        load: _load
    });

});
